$(function() {

    var url = null;//定义地址
    var name = getQueryStringOne("type");
    var pid = getQueryStringOne("pid");
    addAndUpd(name);

    var transfer1 = null;
    var transfer = null;

    function addAndUpd(name) {
        if (name == "add") {
            $('.but').html("添加");
            addSel();
            var src = "../../atsYearMore/selMessage";
            $.post(src, function (response) {
                structure(null,response);
            }, "json");
        }
        if (name == "upd") {
            $('.but').html("修改");
            if (pid != "undefined" && getStrLength(pid) > 0) {
                updateSel();
            }
        }
        url = "../../atsYearMore/addOrUpdate";
    }


    layui.use('form', function(){
        var form = layui.form;
        //监听提交
        form.on('submit(submit_button)', function(data){

            var formData = new FormData(document.getElementById("myForm"));

            if(pid != 'undefined' && pid != null ){
                formData.append("pid",pid);
            }else {
                formData.append("pid",0);
            }

            var coachName = $('#coachId option:selected').text();//获取到教员的名字
            if(coachName!= 'undefined'){
                formData.append("coachName",coachName);
            }else {
                formData.append("coachName",null);
            }

            var name_arry = [];//定义一个装otherCoach的集合
            var getData = transfer1.getData('demo1');//获取穿梭框右边的值
            //其他教员名
            $.each(getData, function (index, item) {
                name_arry.push(item.title);
            });
            if(name_arry!= 'undefined' && name_arry != null){
                formData.append("otherCoach",name_arry);
            }else {
                formData.append("otherCoach",null);
            }

            var itemId_arry = [];//定义一个装itemId的集合
            var itemId=$('[class="itemNextDiv"]');//获取相同class的对象。
            for(var i=0;i<itemId.length;i++){
                var val = $(itemId[i]).attr("id");//获取value值
                if(val != "undefined" && val != null){
                    itemId_arry.push(val);
                }else {
                    itemId_arry.push(0);
                }
            }

            if(itemId_arry!= 'undefined' && itemId_arry != null){
                formData.set("itemIdArry",itemId_arry);
            }else {
                formData.set("itemIdArry",null);
            }

            var itemName_arry = [];//定义一个装itemName的集合
            var itemName=$('[name="itemName"]');//获取相同name的对象。
            for(var i=0;i<itemName.length;i++){
                var val = $(itemName[i]).val();//获取value值
                itemName_arry.push(val);
            }

            if(itemName_arry!= 'undefined' && itemName_arry != null){
                formData.set("itemName",itemName_arry);
            }else {
                formData.set("itemName",null);
            }

            var itemValue_arry = [];//定义一个装itemValue的集合
            var itemValue=$('[name="itemValue"]');//获取相同name的对象。
            for(var i=0;i<itemValue.length;i++){
                var val = $(itemValue[i]).val();//获取value值
                itemValue_arry.push(val);
            }

            if(itemValue_arry!= 'undefined' && itemValue_arry != null){
                formData.set("itemValue",itemValue_arry);
            }else {
                formData.set("itemValue",null);
            }

            //异步请求
            $.ajax({
                url: url,
                data: formData,
                type: "POST",
                dataType: 'json',
                processData: false,
                contentType: false,
                success: function (obj) {
                    if (obj.state == 1) {
                        //关闭当前页，并刷新父页
                        layer.msg(obj.msg, {icon: 1, time: 1000}, function () {
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.location.reload();
                            parent.layer.close(index);
                        });
                    } else {
                        //提示
                        layer.msg(obj.msg);
                    }
                }
            });

            return false;
        });

    });


    function updateSel() {
        var url = "../../atsYearMore/updSel";
        var data = {pid: pid};
        $.post(url, data, function (obj) {

            $("#pname").val(obj.YearMore.pname);//名字
            $("#allTask").val(obj.YearMore.allTask);//总任务
            $("#startYear").val(obj.YearMore.startYear);//开始年份
            $("#endYear").val(obj.YearMore.endYear);//结束年份
            $("#planDescribe").val(obj.YearMore.planDescribe);//计划描述
            $("#isva").val(obj.YearMore.isva);//是否有效
            $("#remark").val(obj.YearMore.remark);//备注

            var src = "../../atsYearMore/selMessage";
            $.post(src, function (response) {
                structure(obj,response);
            },"json");

            $.each(obj.Item, function (index, item) {
                $('#itemDiv').append("<div class=\"itemNextDiv\" id =\""+item.itemId+"\"><div class=\"layui-inline\">\n" +
                    "\t\t\t\t<label class=\"layui-form-label\">指标名称</label>\n" +
                    "\t\t\t\t<div class=\"layui-input-block\">\n" +
                    "\t\t\t\t\t<input type=\"text\" name=\"itemName\" value=\""+item.itemName+"\" lay-verify=\"required\" placeholder=\"请输指标名称\" autocomplete=\"off\" class=\"layui-input\">\n" +
                    "\t\t\t\t</div>\n" +
                    "\t\t\t</div>\n" +
                    "\t\t\t<div class=\"layui-inline\">\n" +
                    "\t\t\t\t<label class=\"layui-form-label\">指标值</label>\n" +
                    "\t\t\t\t<div class=\"layui-input-block\">\n" +
                    "\t\t\t\t\t<input type=\"text\" name=\"itemValue\" value=\""+item.itemValue+"\" lay-verify=\"required\" placeholder=\"请输指标值\" autocomplete=\"off\" class=\"layui-input\">\n" +
                    "\t\t\t\t</div>\n" +
                    "\t\t\t</div>\n" +
                    "\t\t\t<button type=\"button\" class=\"layui-btn closeItem\">\n" +
                    "\t\t\t\t<i class=\"layui-icon\"  >&#x1007;</i> 关闭\n" +
                    "\t\t\t</button></div>");
                renderForm();
            });

        }, "json")
    }

    function addSel(){
        var src = "../../atsYearMore/selMessage";
        $.post(src, function (response) {
            $.each(response.item, function (index, item) {
                $('#itemDiv').append("<div class=\"itemNextDiv\"><div class=\"layui-inline\">\n" +
                    "\t\t\t\t<label class=\"layui-form-label\">指标名称</label>\n" +
                    "\t\t\t\t<div class=\"layui-input-block\">\n" +
                    "\t\t\t\t\t<input type=\"text\" name=\"itemName\" value=\""+item.itemName+"\" lay-verify=\"required\" placeholder=\"请输指标名称\" autocomplete=\"off\" class=\"layui-input\">\n" +
                    "\t\t\t\t</div>\n" +
                    "\t\t\t</div>\n" +
                    "\t\t\t<div class=\"layui-inline\">\n" +
                    "\t\t\t\t<label class=\"layui-form-label\">指标值</label>\n" +
                    "\t\t\t\t<div class=\"layui-input-block\">\n" +
                    "\t\t\t\t\t<input type=\"text\" name=\"itemValue\" value=\""+item.itemValue+"\" lay-verify=\"required\" placeholder=\"请输指标值\" autocomplete=\"off\" class=\"layui-input\">\n" +
                    "\t\t\t\t</div>\n" +
                    "\t\t\t</div>\n" +
                    "\t\t\t<button type=\"button\" class=\"layui-btn closeItem\">\n" +
                    "\t\t\t\t<i class=\"layui-icon\"  >&#x1007;</i> 关闭\n" +
                    "\t\t\t</button></div>");
                renderForm();
            });

        }, "json");
    }

    function structure(temp,message){

        $.each(message.sport, function (index, item) {
            if(temp!=null && temp !="undefined" && item.sportId == temp.YearMore.sportId){
                $('#sportId').append("<option value='"+item.sportId+"' selected='selected'>"+ item.sportName+"</option>");
                renderForm();
            }else {
                $('#sportId').append("<option value='"+item.sportId+"'>"+ item.sportName+"</option>");
                renderForm();
            }
        });
        $.each(message.coach, function (index, item) {
            if(temp!=null && temp !="undefined" && item.coachId == temp.YearMore.coachId){
                $('#coachId').append("<option value='"+item.coachId+"' selected='selected'>"+ item.coachName+"</option>");
                renderForm();
            }else{
                $('#coachId').append("<option value='"+item.coachId+"'>"+ item.coachName+"</option>");
                renderForm();
            }
        });

        layui.use(['transfer', 'layer', 'util'], function() {
            var $ = layui.$
                , layer = layui.layer
                , util = layui.util;
            transfer = layui.transfer;

            //定义标题及数据源
            transfer1=transfer.render({
                elem: '#otherCoach'
                ,title: ['教练人员', '参与教练']  //自定义标题
                ,data: message.coach
                //,width: 150 //定义宽度
                ,height: 250 //定义高度
                ,showSearch: true
                ,id: 'demo1' //定义索引
                ,parseData: function(res){
                    return {
                        "value": res.coachId //数据值
                        ,"title": res.coachName //数据标题
                        ,"disabled": res.disabled  //是否禁用
                        ,"checked": res.checked //是否选中
                    }
                }
            })
            if(temp.otherNameId!= 'undefined' && temp.otherNameId!= null ){
                transfer.reload('demo1', {
                    data: message.coach
                    ,value: temp.otherNameId
                    ,showSearch: true
                })
            }

        });


    }

    var i = 0;
    $("#itemAdd").click(function(){
        i++;
        $("#itemDiv").append("<div class=\"itemNextDiv\"><div class=\"layui-inline\">\n" +
            "\t\t\t\t<label class=\"layui-form-label\">指标名称</label>\n" +
            "\t\t\t\t<div class=\"layui-input-block\">\n" +
            "\t\t\t\t\t<input type=\"text\" name=\"itemName\" id=\"itemName"+i+"\" lay-verify=\"required\" placeholder=\"请输指标名称\" autocomplete=\"off\" class=\"layui-input\">\n" +
            "\t\t\t\t</div>\n" +
            "\t\t\t</div>\n" +
            "\t\t\t<div class=\"layui-inline\">\n" +
            "\t\t\t\t<label class=\"layui-form-label\">指标值</label>\n" +
            "\t\t\t\t<div class=\"layui-input-block\">\n" +
            "\t\t\t\t\t<input type=\"text\" name=\"itemValue\" id=\"itemValue"+i+"\" lay-verify=\"required\" placeholder=\"请输指标值\" autocomplete=\"off\" class=\"layui-input\">\n" +
            "\t\t\t\t</div>\n" +
            "\t\t\t</div>\n" +
            "\t\t\t<button type=\"button\" class=\"layui-btn closeItem\" id=\"closeItem"+i+"\" >\n" +
            "\t\t\t\t<i class=\"layui-icon\"  >&#x1007;</i> 关闭\n" +
            "\t\t\t</button></div>");
        renderForm();
    });

    //删除指标
    $(document).on('click','.closeItem',function(){
        var itemId = $(this).parent().attr("id");
        var item = $(this).parent();

        if(itemId != 'undefined' && itemId != null){
            var url = "../../atsItem/Delete";
            layer.confirm('真的删除这个指标吗?', function(index){
                item.remove();
                $.post(url,{itemId:itemId},function (response) {
                    if(response.state == 1){
                        //提示
                        layer.msg(response.msg);
                    }else{
                        //提示
                        layer.msg(response.msg);
                    }
                });
            });
        }else{
            $(this).parent().remove();;
        }
    });

});

//  重新渲染菜单
function renderForm(){
    layui.use(['form', 'layedit', 'laydate'], function(){
        var form = layui.form
            ,layer = layui.layer
            ,layedit = layui.layedit
            ,laydate = layui.laydate;
        form.render();
        laydate.render({
            elem: '#startYear'
            ,trigger: 'click' //采用click弹出
            ,type: 'year'
        });
        laydate.render({
            elem: '#endYear'
            ,trigger: 'click' //采用click弹出
            ,type: 'year'
        });
    });
}
出
// 得到地址栏上的参数 可以 是中文
function getQueryStringOne(key) {
	var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if (r != null)
		return decodeURI(r[2]);
	return null;
}

//得到字符串长度
function getStrLength(str) {
	var cArr = str.match(/[^\x00-\xff]/ig);
	return str.length + (cArr == null ? 0 : cArr.length);
}
